﻿using System;
using System.Windows.Forms;
using System.Threading;
using System.Reflection;

namespace ChargingSystem4.CsForm
{
    public interface ISplashForm
    {
        void SetTitleInfo(string NewStatusInfo);
        void SetStatusInfo(string NewStatusInfo);
        void SetProBarValue(int value);
        void SetProBarMaxValue(int value);
        void SetProBarStyle(string flag);
    }
    /// <summary>
    /// Splasher类，该类包含Show、 Close 两个静态方法以显示、关闭启动屏幕，另外还有一个名为Status的只读属性
    /// </summary>
    public class Splasher
    {
        private static Form m_SplashForm = null;
        private static ISplashForm m_SplashInterface = null;
        private static Thread m_SplashThread = null;
        private static string m_TempStatus = string.Empty;
        private static string m_TempTitle = string.Empty;
        private static int m_ProBarMaxValue = 0;
        private static int m_ProBarValue = 0;
        private static string m_StyleFlag = string.Empty;

        private delegate void SplashTitleHandle(string NewStatusInfo);
        private delegate void SplashStatusChangedHandle(string NewStatusInfo);
        private delegate void SplashProBarMaxValueCallBack(int intValue);
        private delegate void SplashProBarValueCallBack(int intValue);
        private delegate void SplashProBarStyleCallBack(string flag);

        /// <summary>
        /// 显示启动画面
        /// </summary>
        public static void Show(Type splashFormType)
        {
            if (m_SplashThread != null)
                return;
            if (splashFormType == null)
            {
                throw (new Exception("必须设置启动窗体"));
            }

            m_SplashThread = new Thread(
            new ThreadStart(delegate
            {
                CreateInstance(splashFormType);
                Application.Run(m_SplashForm);
            }
            ));
            m_SplashThread.IsBackground = true;
            m_SplashThread.SetApartmentState(ApartmentState.STA);
            m_SplashThread.Start();
        }

        /// <summary>
        /// 设置加载信息
        /// </summary>
        public static string Status
        {
            set
            {
                try
                {
                    if (m_SplashInterface == null || m_SplashForm == null)
                    {
                        m_TempStatus = value;
                        return;
                    }
                    m_SplashForm.Invoke(
                    new SplashStatusChangedHandle(delegate (string str)
                    {
                        m_SplashInterface.SetStatusInfo(str);
                    }),
                    new object[] { value }
                    );
                }
                catch { }
            }
        }

        /// <summary>
        /// 设置加载信息
        /// </summary>
        public static string Title
        {
            set
            {
                if (m_SplashInterface == null || m_SplashForm == null)
                {
                    m_TempTitle = value;
                    return;
                }
                //m_SplashForm.Invoke(
                //new SplashTitleHandle(delegate (string str)
                //{
                //    m_SplashInterface.SetTitleInfo(str);
                //}),
                //new object[] { value }
                //);
            }
        }




        /// <summary>
        /// 进度条最大值
        /// </summary>
        public static int ProBarMaxValue
        {
            set
            {
                if (m_SplashInterface == null || m_SplashForm == null)
                {
                    m_ProBarMaxValue = value;
                    return;
                }
                m_SplashForm.Invoke(new SplashProBarMaxValueCallBack(delegate (int intValue)
                {
                    m_SplashInterface.SetProBarMaxValue(intValue);
                }), new object[] { value });
            }
        }

        /// <summary>
        /// 进度条当前值
        /// </summary>
        public static int ProBarValue
        {
            set
            {
                if (m_SplashInterface == null || m_SplashForm == null)
                {
                    m_ProBarValue = value;
                    return;
                }
                m_SplashForm.Invoke(new SplashProBarValueCallBack(delegate (int intValue)
                {
                    m_SplashInterface.SetProBarValue(intValue);
                }), new object[] { value });
            }
        }

        /// <summary>
        /// 进度条样式,3表示ProgressBarStyle.Marquee（枚举3表不停的走动）
        /// </summary>
        public static string StypeFlag
        {
            set
            {
                if (m_SplashInterface == null || m_SplashForm == null)
                {
                    m_StyleFlag = value;
                    return;
                }
                m_SplashForm.Invoke(new SplashProBarStyleCallBack(delegate (string strFlag)
                {
                    m_SplashInterface.SetProBarStyle(strFlag);
                }), new object[] { value });
            }
        }

        /// <summary>
        /// 关闭启动画面
        /// </summary>
        public static void Close()
        {
            if (m_SplashThread == null || m_SplashForm == null)
                return;

            try
            {
                m_SplashForm.Invoke(new MethodInvoker(m_SplashForm.Close));
            }
            catch (Exception)
            {
            }
            m_SplashThread = null;
            m_SplashForm.Dispose();
            m_SplashForm = null;
        }

        private static void CreateInstance(Type FormType)
        {

            object obj = FormType.InvokeMember(null,
            BindingFlags.DeclaredOnly |
            BindingFlags.Public | BindingFlags.NonPublic |
            BindingFlags.Instance | BindingFlags.CreateInstance, null, null, null);
            m_SplashForm = obj as Form;
            m_SplashInterface = obj as ISplashForm;
            if (m_SplashForm == null)
            {
                throw (new Exception("启动窗体类型必须是System.Windows.Forms.Form的子类"));
            }
            if (m_SplashInterface == null)
            {
                throw (new Exception("启动窗体必须实现ISplashForm接口"));
            }

            if (!string.IsNullOrEmpty(m_TempStatus))
                m_SplashInterface.SetStatusInfo(m_TempStatus);

            if (!string.IsNullOrEmpty(m_TempTitle))
                m_SplashInterface.SetTitleInfo(m_TempTitle);

        }
    }


}
